package com.samuel.aspect;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @date:2017/11/21
 * @time: 15:23
 * @author:zhaoyang
 * @desc:切面
 **/
@Aspect
@Component
public class LogAspect {


    /**
     * @Author: taozhaoyang
     * @Description: @Pointcut("@annotation(com.samuel.aspect.Action)") 注解接口的切入点
     * 只对使用@Action的方法有效
     * @Date: 2017/11/21 18:02
     */
    @Pointcut("@annotation(com.samuel.aspect.Action)")
    public void annocationPointCut() {

    }


    /**
     * @Author: taozhaoyang
     * @Description: @After 在切入的方法执行完生效
     * @Date: 2017/11/21 18:04
     */
   /* @Before("annocationPointCut()")
    public void before(JoinPoint joinPoint) {
        System.out.println("--------进入 before");
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        Action action = method.getAnnotation(Action.class);
        if (action == null) {
            System.out.println("注解式拦截：" + method.getName());
            return;
        }
        System.out.println("注解式拦截：" + action.name());
    }*/


    @AfterReturning(value = "annocationPointCut()",returning = "retVal")
    public void afterReturning(JoinPoint joinPoint,Object retVal) {
        System.out.println("afterReturning："+joinPoint.getSignature());
        System.out.println(retVal);
    }

    @After("annocationPointCut()")
    public void after(JoinPoint joinPoint) {
        System.out.println("--------进入 after");
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        Action action = method.getAnnotation(Action.class);
        if (action == null) {
            System.out.println("注解式拦截：" + method.getName());
            return;
        }
        System.out.println("注解式拦截：" + action.name());
    }


/**
 * @Author: taozhaoyang
 * @Description:  @Before 在 进入  切入的方法 前 执行
 * @Date: 2017/11/21 18:04
 */
/*    @Before("execution(* com.samuel.aspect.DemoMethodService.*(..))")
    public void  before(JoinPoint joinPoint){
        System.out.println("--------进入 before");
        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
        Method method =  methodSignature.getMethod();
        System.out.println("方法规则式拦截：" + method.getName());

    }*/
}
